package yang.yu.sunny.eventhandler;

import com.google.common.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import yang.yu.sunny.domain.*;

/**
 * Created by yyang on 16/3/24.
 */
public class OrderEventHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderEventHandler.class);

    @Subscribe
    public void productCategoryNameChanged(ProductCategoryNameChangedEvent event) {
        OrderItem2.findByProperty(OrderItem2.class, "categoryId", event.getProductCategoryId()).stream()
                .forEach(orderItem2 -> {
                    orderItem2.setCategoryName(event.getNewName());
                    orderItem2.save();
                });
        LOGGER.info("Product name in OrderItem2 changed!");
    }

    @Subscribe
    public void productNameChanged(ProductNameChangedEvent event) {
        OrderItem2.findByProperty(OrderItem2.class, "productId", event.getProductId()).stream()
                .forEach(orderItem2 -> {
                    orderItem2.setProductName(event.getNewName());
                    orderItem2.save();
                });
        LOGGER.info("Product name in OrderItem2 changed!");
    }

    @Subscribe
    public void customerNameChanged(CustomerNameChangedEvent event) {
        Order2.findByProperty(Order2.class, "customerId", event.getCustomerId()).stream()
                .forEach(order2 -> {
                    order2.setCustomerName(event.getNewName());
                    order2.save();
                });
        LOGGER.info("Customer name in Order2 changed!");
    }
}
